export function updateApp() {
  // #ifdef MP
  // 微信小程序更新提醒
  if (wx.canIUse("getUpdateManager")) {
    const updateManager = wx.getUpdateManager();
    updateManager.onCheckForUpdate(function (res) {
      if (res.hasUpdate) {
        updateManager.onUpdateReady(function () {
          wx.showModal({
            title: "更新提示",
            content: "新版本已经准备好，是否重启应用？",
            success: function (res) {
              if (res.confirm) {
                updateManager.applyUpdate();
              }
            },
          });
        });
        updateManager.onUpdateFailed(function () {
          wx.showModal({
            title: "已经有新版本了哟~",
            content: "新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~",
          });
        });
      }
    });
  } else {
    wx.showModal({
      title: "提示",
      content: "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。",
    });
  }
  // #endif
}

export function getQuery(name: string) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}

//设置标题

export function setTitle(title: string) {
  ZWJSBridge.onReady(() => {
    ZWJSBridge.setTitle({
      title,
    });
  });
}

//获取申请表 评定表
import {
  getApplyTablePdf,
  getAssessTablePdf,
  getVisionTablePdf,
  getHearingTablePdf,
  getSpeechTablePdf,
  getLimbPdf,
  getIntelligencePdf,
  getSpiritTablePdf,
} from "../api/index";

// 将返回的流数据转换为url
function getObjectURL(file) {
  let url = null;
  if (window.createObjectURL != undefined) {
    // basic
    url = window.createObjectURL(file);
  } else if (window.webkitURL != undefined) {
    // webkit or chrome
    try {
      url = window.webkitURL.createObjectURL(file);
    } catch (error) {}
  } else if (window.URL != undefined) {
    // mozilla(firefox)
    try {
      url = window.URL.createObjectURL(file);
    } catch (error) {}
  }
  return url;
}
export async function getApplyTableFnc(matterId: string) {
  let data = await getApplyTablePdf(matterId);
  let url = getObjectURL(data);
  return url;
}

export async function getAssessTable(type: number, matterId: string) {
  let data, url;
  if (type == 100) {
    data = await getAssessTablePdf(matterId);
    url = getObjectURL(data);
  } else if (type == 1) {
    data = await getVisionTablePdf(matterId);
    url = getObjectURL(data);
  } else if (type == 2) {
    data = await getHearingTablePdf(matterId);
    url = getObjectURL(data);
  } else if (type == 3) {
    data = await getSpeechTablePdf(matterId);
    url = getObjectURL(data);
  } else if (type == 4) {
    data = await getLimbPdf(matterId);
    url = getObjectURL(data);
  } else if (type == 5) {
    data = await getIntelligencePdf(matterId);
    url = getObjectURL(data);
  } else if (type == 6) {
    data = await getSpiritTablePdf(matterId);
    url = getObjectURL(data);
  }
  if (type == 200) {
    data = await getApplyTableFnc(matterId);
    url = getObjectURL(data);
  }
  uni.navigateTo({
    url: "/pages/pdf/index?url=" + url,
  });
  // return `${encodeURIComponent(url)}`
}

//判断身份证号是否年满18周岁
export function getIdcardAge(idCard) {
  //根据身份证号计算出生日期
  function getBirthDate(IDCard) {
    //获取身份证号的年、月、日
    var year = IDCard.substring(6, 10);
    var month = IDCard.substring(10, 12);
    var day = IDCard.substring(12, 14);
    //拼接成出生日期
    var birthDate = year + "-" + month + "-" + day;
    return birthDate;
  }

  //根据两个日期计算年龄（是否过生日）
  function getAge(startDateStr, endDateStr) {
    //计算两个日期相差多少年
    var startDate = new Date(startDateStr);
    var endDate = new Date(endDateStr);
    var yearNum = endDate.getFullYear() - startDate.getFullYear();
    if (yearNum == 18 || yearNum == 3 || yearNum == 2) {
      //获取两个日期（月+日）部分
      var sStr = startDateStr.substring(5, 10);
      var eStr = endDateStr.substring(5, 10);

      //判断两个日期大小
      //判断是否过生日
      if (new Date(sStr) <= new Date(eStr)) {
        return yearNum;
      } else {
        return yearNum - 1;
      }
    } else {
      return yearNum;
    }
  }

  ///获取当前日期
  function getNowDate() {
    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    var day = d.getDate();
    var dateStr = year + "-" + getFormatDate(month) + "-" + getFormatDate(day);
    return dateStr;
  }
  //格式化日期的月份或天数的显示（小于10，在前面增加0）
  function getFormatDate(value) {
    if (value == undefined || value == "") {
      return "";
    }
    var str = value;
    if (parseInt(value) < 10) {
      str = "0" + value;
    }
    return str;
  }
  //获取计算后出生日期
  let birthDateStr = getBirthDate(idCard);
  //获取当前的日期
  let nowDateStr = getNowDate();
  //获取计算后年龄(两个日期之间)
  let age = getAge(birthDateStr, nowDateStr);
  return age;
}

//对象转链接
export function jointUrl(obj: object) {
  const paramArr = [];
  let param = "";

  if (obj && typeof obj === "object") {
    if (paramArr.length === 0) {
      paramArr.push("?");
    }
    for (const i in obj) {
      if (obj.hasOwnProperty(i)) {
        if (obj[i] !== "" && obj[i] !== null && typeof obj[i] !== "undefined") {
          paramArr.push(i);
          paramArr.push("=");
          paramArr.push(obj[i]);
          paramArr.push("&");
        }
      }
    }
    param = paramArr.join("").replace(/&$/, "");
    return param;
  }
}

//获取链接参数
export function GetQueryString(name) {
  var reg = new RegExp("[?&]" + name + "=([^&^#^/]*)", "i");
  var res = window.location.href.match(reg);
  return res != null ? unescape(res[1]) : "";
}


// 数组去除空值
export function arrayExculdNull(arr){
  return arr.filter((s) => {
    return s && s.trim();     // 注：IE9(不包含IE9)以下的版本没有trim()方法
  });
}


